# 统一日志类模块

import logging
import os
from datetime import datetime

class Logger:
    _instance = None
    _initialized = False
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(Logger, cls).__new__(cls)
        return cls._instance
    
    def __init__(self):
        if not self._initialized:
            # 确保日志目录存在
            log_dir = os.path.join(os.getcwd(), "logs")
            os.makedirs(log_dir, exist_ok=True)
            
            # 设置日志文件名，按日期命名
            current_date = datetime.now().strftime("%Y-%m-%d")
            log_file = os.path.join(log_dir, f"liber_tool_{current_date}.log")
            
            # 配置日志
            self.logger = logging.getLogger("liber_tool")
            self.logger.setLevel(logging.INFO)
            
            # 文件处理器
            file_handler = logging.FileHandler(log_file, encoding="utf-8")
            file_handler.setLevel(logging.INFO)
            
            # 控制台处理器
            console_handler = logging.StreamHandler()
            console_handler.setLevel(logging.INFO)
            
            # 设置日志格式
            formatter = logging.Formatter(
                "%(asctime)s - %(levelname)s - %(module)s - %(message)s"
            )
            file_handler.setFormatter(formatter)
            console_handler.setFormatter(formatter)
            
            # 添加处理器
            self.logger.addHandler(file_handler)
            self.logger.addHandler(console_handler)
            
            self._initialized = True
    
    def info(self, message):
        self.logger.info(message)
    
    def error(self, message):
        self.logger.error(message)
    
    def warning(self, message):
        self.logger.warning(message)
    
    def debug(self, message):
        self.logger.debug(message)
    
    def critical(self, message):
        self.logger.critical(message)

# 创建单例实例
logger = Logger()

# 导出函数，方便直接调用
def info(message):
    logger.info(message)

def error(message):
    logger.error(message)

def warning(message):
    logger.warning(message)

def debug(message):
    logger.debug(message)

def critical(message):
    logger.critical(message)